package com.gitee.config;

import io.minio.MinioClient;
import io.minio.policy.PolicyType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfig {


    @Value("${minio.endPoint}")
    private String endPoint;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    @Value("${minio.bucketName}")
    private String bucketName;


    @Bean
    public MinioClient minioClient() throws Exception {
        MinioClient minioClient = new MinioClient(endPoint, accessKey, secretKey);
        boolean isExist = minioClient.bucketExists(bucketName);
        if(!isExist) {
            minioClient.makeBucket(bucketName);
            minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_WRITE);
        }
        return minioClient;
    }

}
